package fr.umlv.m2.jee.pmlv.persistence.planning.dao;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.stereotype.Repository;

import fr.umlv.m2.jee.pmlv.framework.dao.hibernate.AbstractHibernateDAO;
import fr.umlv.m2.jee.pmlv.persistence.planning.RecordDate;
import java.util.ArrayList;

/**
 * DAO for the class {@link RecordDate} with the framework Hibernate.
 * This class implements {@link IRecordDateDAO}
 * @author jenseric
 *
 */
@Repository("hibernateRecordDateDAO")
public class HibernateRecordDateDAO extends AbstractHibernateDAO<Long, RecordDate> implements IRecordDateDAO {

	@Override
	public List<RecordDate> findByWeekAndByProfessorUidAndByYear(GregorianCalendar start, String uid, int year) {
                GregorianCalendar tmp = (GregorianCalendar)start.clone();
		List<RecordDate>  list = new ArrayList<RecordDate>();
		for(int i = 0;i < 7 ;i ++) {
			String query = "select distinct rd from RecordDate as rd, UserSubject as us, AbstractTheme as theme "
				+ "where us.primaryKey.subject.id = theme.subject.id and theme.id = rd.theme.id and us.primaryKey.year="+year+" " 
				+ "and us.primaryKey.uid='"+uid+"' and year(rd.deadLine)="+tmp.get(Calendar.YEAR)+" and month(rd.deadLine)="+(tmp.get(Calendar.MONTH)+1)+"and day(rd.deadLine)="+tmp.get(Calendar.DATE);
			tmp.add(Calendar.DATE, 1);
			list.addAll(findByQuery(query));
		}
		return list;
	}

	@Override
	public List<RecordDate> findByWeekAndByStudentUidAndByYear(GregorianCalendar start, String uid, int year) {
            GregorianCalendar tmp = (GregorianCalendar)start.clone();
		List<RecordDate>  list = new ArrayList<RecordDate>();
		for(int i = 0;i < 7 ;i ++) {
			String query = "select distinct rd from RecordDate as rd, UserSpecialty as us, SpecialtySubject as spe , AbstractTheme as theme "
				+ "where us.primaryKey.specialty.id = spe.specialty.id and spe.subject.id = theme.subject.id  and theme.id = rd.theme.id and us.primaryKey.year="+year+" " 
				+ "and us.primaryKey.uid='"+uid+"' and year(rd.deadLine)="+tmp.get(Calendar.YEAR)+" and month(rd.deadLine)="+(tmp.get(Calendar.MONTH)+1)+"and day(rd.deadLine)="+tmp.get(Calendar.DATE);
			tmp.add(Calendar.DATE, 1);
			list.addAll(findByQuery(query));
		} 
		return list;
	}

	@Override
	public List<RecordDate> findByWeekAndBySecretaryUidAndByYear(GregorianCalendar start, String uid, int year) {
            GregorianCalendar tmp = (GregorianCalendar)start.clone();
		List<RecordDate>  list = new ArrayList<RecordDate>();
		for(int i = 0;i < 7 ;i ++) {
			String query = "select distinct rd from RecordDate as rd, UserCursus as us, SpecialtySubject as spe, AbstractTheme as theme "
				+ "where us.primaryKey.cursus.id = spe.specialty.cursus.id and spe.subject.id = theme.subject.id  and theme.id = rd.theme.id and us.primaryKey.year="+year+" "
				+ "and us.primaryKey.uid='"+uid+"' and year(rd.deadLine)="+tmp.get(Calendar.YEAR)+" and month(rd.deadLine)="+(tmp.get(Calendar.MONTH)+1)+"and day(rd.deadLine)="+tmp.get(Calendar.DATE);
			tmp.add(Calendar.DATE, 1);
			list.addAll(findByQuery(query));
		} 
		return list;
	}
}



